Skip to content


ardupilot  None  ros2  dds  micro ros  xrce  sitl  plugin  SITL  debug  rangefinder  pymavlink  mavros  gazebo  distance sensor  system_time  timesync  cmake  gtest  ctest  101  cpp  c++  format  fmt  multithreading  spdlog  cyclonedds  eprosima  fastdds  simulation  config  ignition  bridge  sdf  tips  ign-transport  camera  sensors  lidar  aptly  apt  repository  repo  local  mirror  encryption  pgp  docker  nvidia  git  bundle  submodules  github  hooks  pre-commit  lxd  container  lxc  x11  profile  vscode  marpit  presentation  marp  markdown  mermaid  mkdocs  video  ffmpeg  gstreamer  cheat-sheet  sdp  v4l2loopback  gi  kml  python  geo  snippets  cheat Sheet  asyncio  future  click  cli  numpy  project  template  black  isort  templates  cookiecutter  docs  project document  docstrings  flake8  linter  git-hook  mypy  unittest  pytest  pylint  from a-z  mock  iterator  generator  logging  tuple  namedtuple  typing  annotation  typever  pyzmq  zmq  msgpack  action  namespace  remap  control2  ros2_control  effort  velocity  gdb  qos  tag  plugins  msg  node  zero-copy  shm  tutorial  algorithm  calibration  diff  pid  dev  colcon  colcon_cd  rpi  arm  qemu  settings  behavior  py_trees  bt  behavior_trees  blackboard  plot  visualization  debugging  diagnostic  DiagnosticTask  diagnostics  tutorials  gst  math  apm  rat_runtime_monitor  web  rosbridge  vue  binding  discovery  gazebo-classic  launch  spawn  model  cook  gps  imu  ray  gazebo_ros_ray_sensor  ultrsonic  range  ultrasonic  gazebo classic  wrench  odom  ign  gz  xacro  ros_ign  diff_drive  odometry  joint_state  argument  OpaqueFunction  DeclareLaunchArgument  LaunchConfiguration  tmux  nav  slam  test  rclpy  goal abort  cancel goal  action client  action server  custom messages  executor  MultiThreadedExecutor  SingleThreadedExecutor  param  dynamic-reconfigure  service  client  setup.py  package.xml  parameter  parameters  custom  msgs  executers  pub  sub  rqt  rviz  rviz2  pose  marker  tf2  deb  package  setup  local_setup  rosdep  package manager  project settings  vcstool  urdf  robot_state_publisher  urdf_to_graphiz  joint  link  cross-compiler  nano  texture  tmuxp  loop device  rootfs  embedded  zah  linux  rm  ubuntu  sudo  sudoers  nopasswd  visudo  udev  key  gpg  sign  ip  ss  network  netstat  snap  deploy  ssh  systemd  socat  networking  serial  udp  tc  mtu  select  robotics  kalman_filter  kalman  filter  control  extensions  json  yaml  schema  yocto  poky  projects  courses to follow  world  gazebo_ros2_control  position_controller  effort_controller  velocity_controller  gazebo_ros_force  gazebo_ros_joint_state_publisher  joint_state_publisher  vrx  buoyancy 

projects
rrbot_gazebo
    ├── CMakeLists.txt
    ├── launch
       └── world_plug.launch.py
    ├── package.xml
    ├── src
       └── simple_world_plugin.cpp
    └── worlds
        └── world_plug.world 

Worlds#

world_plug.world
<?xml version="1.0" ?>
<sdf version="1.4">
  <!-- We use a custom world for the rrbot so that the camera angle is launched correctly -->

  <world name="default">
    <include>
      <uri>model://ground_plane</uri>
    </include>

    <!-- Global light source -->
    <include>
      <uri>model://sun</uri>
    </include>
    <plugin name="simple" filename="libsimple_world_plugin.so"/>
  </world>
</sdf>

package.xml#

<export>
    <build_type>ament_cmake</build_type>
    <gazebo_ros plugin_path="${prefix}"></gazebo_ros>
</export>

plugin source#

simple_world_plugin.cpp
#include <gazebo/common/Plugin.hh>
#include <rclcpp/rclcpp.hpp>
#include <iostream>

namespace gazebo
{
class WorldPluginTutorial : public WorldPlugin
{
public:
  WorldPluginTutorial() : WorldPlugin()
  {
    // gazebo log
    gzmsg << "gazebo message" << std::endl;
    gzwarn << "gazebo warning" << std::endl;
    gzerr << "gazebo error" << std::endl;

    // ROS log
    RCLCPP_INFO(rclcpp::get_logger("world_plug")," ------ Hello World! ------ ");
    RCLCPP_WARN(rclcpp::get_logger("world_plug")," ------ warning! ------ ");
    RCLCPP_ERROR(rclcpp::get_logger("world_plug")," ------ Error! ------ ");
  }

  void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf)
  {
  }

};
GZ_REGISTER_WORLD_PLUGIN(WorldPluginTutorial)
}

CMakeLists#

cmake_minimum_required(VERSION 3.5)
project(rrbot_gazebo)

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(gazebo REQUIRED)
find_package(gazebo_ros REQUIRED)
find_package(rclcpp REQUIRED)

add_library(simple_world_plugin SHARED src/simple_world_plugin.cpp)

target_include_directories(simple_world_plugin PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>)

ament_target_dependencies(simple_world_plugin
  "gazebo_ros"
  "rclcpp"
  )

install(TARGETS
simple_world_plugin
  DESTINATION share/${PROJECT_NAME})

install(DIRECTORY
  worlds
  DESTINATION share/${PROJECT_NAME}
)

install(DIRECTORY
  launch
  DESTINATION share/${PROJECT_NAME}
)

ament_package()

launch#

world_plug.launch.py
import os

from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource

package_name = 'rrbot_gazebo'
world_file = 'world_plug.world'

def generate_launch_description():

    pkg_gazebo_ros = get_package_share_directory('gazebo_ros')
    pkg_simulation = get_package_share_directory(package_name)

    # launch Gazebo by including its definition
    gazebo = IncludeLaunchDescription(
        PythonLaunchDescriptionSource(
            os.path.join(pkg_gazebo_ros, 'launch', 'gazebo.launch.py'),
        )
    )
    verbose_arg = DeclareLaunchArgument(
            'verbose', default_value='true',
            description='Set "true" to increase messages written to terminal.'
        )

    # load the world file
    world_arg = DeclareLaunchArgument(
          'world',
          default_value=[os.path.join(pkg_simulation, 'worlds', world_file), ''],
          description='SDF world file')

    return LaunchDescription([
        world_arg,
        verbose_arg,
        gazebo,

    ])

Usage and Run#

ros2 launch rrbot_gazebo world_plug.launch.py
  • The first three log line came from gazebo log API
  • The Other three came from ROS log API